const API = require("../../../utils/api")
const app = getApp()
const canvasfun = require("./createPoster")
// pages/park/visiting-card/visiting-card.js
Page({
    ...canvasfun,
    /**
     * 页面的初始数据
     */
    data: {
        cardId: '',
        tempImage: '',
        modeShow: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.data.cardId = options.id || ''
    },
    onShow: function () {
        this.data.cardId ? this.get_card_detail() : this.get_user_card_detail()
    },
    editCardEvent() {
        const { cardDetail } = this.data
        wx.navigateTo({ url: `/pages/mine/park-manage/card/edit?cardId=${cardDetail.id}` })
    },
    authorEvent() {
        this.setData({
            modeShow: !this.data.modeShow
        })
    },
    bindlongtap() {
        wx.saveImageToPhotosAlbum({
            filePath: this.data.tempImage,
            success: (res) => {
                wx.showToast({
                    title: '保存成功',
                    duration: 1000,
                })
                this.setData({
                    modeShow: false
                })
            }
        })
    },
    async create_poster() {
        let query = wx.createSelectorQuery();
        query.select('.mycanvas').boundingClientRect(async (canvasInfo) => {
            const context = wx.createCanvasContext('mycanvas');
            const res = await this.createPoster(this.data.cardDetail, context, canvasInfo);
            this.setData({ tempImage: res })
        }).exec();
    },
    /**
     * 接口 - 查询名片详情
     */
    get_card_detail: function () {
        let that = this
        API.get_card_detail({
            id: that.data.cardId
        }).then(res => {
            if (res.code == 200) {
                let cardDetail = res.data
                if (cardDetail.phone) {
                    cardDetail.phoneShow = cardDetail.phone.substring(0, 3) + '****' + cardDetail.phone.substring(7, 11)
                }
                let siteIndex = ''
                if (cardDetail.site) {
                    siteIndex = cardDetail.site.indexOf('市')
                    siteIndex != -1 ? cardDetail.site = cardDetail.site.substr(siteIndex) : ''
                }
                console.log(siteIndex)
                this.setData({
                    cardDetail: cardDetail
                })
                this.create_poster()
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'none'
                })
            }
        })
    },

    /**
    * 接口 - 查询当前用户名片详情
    */
    get_user_card_detail: function () {
        let that = this
        API.get_user_card_detail().then(res => {
            if (res.code == 200) {
                let cardDetail = res.data
                if (cardDetail.phone) {
                    cardDetail.phoneShow = cardDetail.phone.substring(0, 3) + '****' + cardDetail.phone.substring(7, 11)
                }
                let siteIndex = ''
                if (cardDetail.site) {
                    siteIndex = cardDetail.site.indexOf('市')
                    siteIndex != -1 ? cardDetail.site = cardDetail.site.substr(siteIndex) : ''
                }
                console.log(siteIndex)
                this.setData({
                    cardDetail: cardDetail
                })
                this.create_poster()
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'none'
                })
            }
        })
    },

    /**
     * 拨打电话
     */
    callPhone: function () {
        wx.makePhoneCall({
            phoneNumber: this.data.cardDetail.phone,
        })
    },

    /**
     * 添加微信
     */
    addwx: function () {
        // console.log('45453',this.data.cardDetail)
        app.contact(this.data.cardDetail.wxYard)
    },

    /**
     * 返回企业首页
     */
    back: function () {
        wx.navigateBack({
            delta: 1,
        })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})